/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package presentation;

import entity.Client;
import entity.Commande;
import entity.LignCmd;
import java.awt.Color;
import java.beans.Beans;
import java.util.AbstractCollection;
import java.util.Collection;
import java.util.Date;
import java.util.LinkedList;
import java.util.List;
import java.util.Vector;
import javax.persistence.EntityManager;
import javax.swing.BorderFactory;
import javax.swing.JOptionPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableColumnModel;
import javax.swing.table.TableModel;

/**
 *
 * @author Khalid
 */
public class CommandeDetaillesForm extends javax.swing.JFrame {

    /**
     * Creates new form CommandeDetaillesForm
     */
    private final static double PARPA = 7.3;
    private final static double LOURDI = 0.62;

    private entity.Commande currentCommande;
    private javax.persistence.EntityManager entityManager;

    public CommandeDetaillesForm() {

        entityManager = java.beans.Beans.isDesignTime() ? null : javax.persistence.Persistence.createEntityManagerFactory("gestionPU").createEntityManager();
        if (!Beans.isDesignTime()) {
            entityManager.getTransaction().begin();
        }
        //this.currentCommande = entityManager.find(entity.Commande.class, 1);
        //this.currentCommande.setLignCmdCollection((List)new LinkedList<LignCmd>());
        initComponents();
        this.enableComponent();
        if (currentCommande == null) {
            this.currentCommande = new Commande();
            this.createJtable();
        }
        if (jTable1.getSelectedRows().length == 0) {
            cmbPdt.setSelectedIndex(-1);
        }
        fillForm(this.currentCommande);

        if (this.currentCommande == null) {
            cmbClient.setSelectedIndex(-1);
        }
        lblMsg.setVisible(false);
    }

    void enableComponent() {
        cmbClient.setEnabled(this.getCurrentCommande() == null);
        jdate.setEnabled(this.getCurrentCommande() == null);
        txtBenefice.setEnabled(this.getCurrentCommande() == null);
        txtCredit.setEnabled(this.getCurrentCommande() == null);
        txtTotal.setEnabled(this.getCurrentCommande() == null);
        jTable1.setEnabled(this.getCurrentCommande() == null);
        btnValid.setEnabled(this.getCurrentCommande() == null);
    }

    void fillForm(entity.Commande c) {
        if (c == null) {
            return;
        }
        txtRef.setText(c.getRef());
        txtBenefice.setValue(c.getBenefice());
        txtCredit.setValue(c.getCredit());
        txtTotal.setValue(c.getTotal());
        txtsomme.setValue(c.getSommePaye());
        cmbClient.setSelectedItem(c.getClientId());
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {
        bindingGroup = new org.jdesktop.beansbinding.BindingGroup();

        gestionPUEntityManager = java.beans.Beans.isDesignTime() ? null : javax.persistence.Persistence.createEntityManagerFactory("gestionPU").createEntityManager();
        clientQuery = java.beans.Beans.isDesignTime() ? null : gestionPUEntityManager.createQuery("SELECT c FROM Client c");
        clientList = java.beans.Beans.isDesignTime() ? java.util.Collections.emptyList() : clientQuery.getResultList();
        produitQuery = java.beans.Beans.isDesignTime() ? null : gestionPUEntityManager.createQuery("SELECT p FROM Produit p");
        produitList = java.beans.Beans.isDesignTime() ? java.util.Collections.emptyList() : produitQuery.getResultList();
        lignCmdQuery = java.beans.Beans.isDesignTime() ? null : gestionPUEntityManager.createQuery("SELECT l FROM LignCmd l");
        lignCmdList = java.beans.Beans.isDesignTime() ? java.util.Collections.emptyList() : lignCmdQuery.getResultList();
        refreshButton = new javax.swing.JButton();
        lignCmdQuery1 = java.beans.Beans.isDesignTime() ? null : gestionPUEntityManager.createQuery("SELECT l FROM LignCmd l");
        lignCmdList1 = java.beans.Beans.isDesignTime() ? java.util.Collections.emptyList() : lignCmdQuery1.getResultList();
        jPanel2 = new javax.swing.JPanel();
        clientIdLabel = new javax.swing.JLabel();
        refLabel = new javax.swing.JLabel();
        txtRef = new javax.swing.JTextField();
        dateCmdLabel = new javax.swing.JLabel();
        jdate = new javax.swing.JSpinner();
        cmbClient = new javax.swing.JComboBox();
        produitListCellRenderer1 = new presentation.ProduitListCellRenderer();
        jPanel4 = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTable1 = new javax.swing.JTable();
        cmbPdt = new javax.swing.JComboBox();
        lblpdt = new javax.swing.JLabel();
        txtHauteur = new javax.swing.JTextField();
        txtLargeur = new javax.swing.JTextField();
        txtQte = new javax.swing.JTextField();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        lblQte = new javax.swing.JLabel();
        btnValid = new javax.swing.JButton();
        btnValid1 = new javax.swing.JButton();
        lblMsg = new javax.swing.JLabel();
        jPanel3 = new javax.swing.JPanel();
        totalLabel = new javax.swing.JLabel();
        sommePayeLabel = new javax.swing.JLabel();
        creditLabel = new javax.swing.JLabel();
        beneficeLabel = new javax.swing.JLabel();
        txtTotal = new javax.swing.JFormattedTextField();
        txtsomme = new javax.swing.JFormattedTextField();
        txtBenefice = new javax.swing.JFormattedTextField();
        txtCredit = new javax.swing.JFormattedTextField();
        deleteButton = new javax.swing.JButton();
        deleteButton1 = new javax.swing.JButton();
        deleteButton2 = new javax.swing.JButton();

        refreshButton.setText("Refresh");
        refreshButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                refreshButtonActionPerformed(evt);
            }
        });

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jPanel2.setBorder(javax.swing.BorderFactory.createTitledBorder("Information sur la commande"));

        clientIdLabel.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        clientIdLabel.setText("Client: ");

        refLabel.setFont(new java.awt.Font("Tahoma", 1, 24)); // NOI18N
        refLabel.setText("Ref:");

        txtRef.setEditable(false);
        txtRef.setFont(new java.awt.Font("Tahoma", 1, 24)); // NOI18N

        dateCmdLabel.setText("Date  de création: ");

        jdate.setModel(new javax.swing.SpinnerDateModel());
        jdate.setEditor(new javax.swing.JSpinner.DateEditor(jdate, "dd/MM/yyyy"));

        org.jdesktop.beansbinding.Binding binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, this, org.jdesktop.beansbinding.ELProperty.create("${currentCommande != null}"), jdate, org.jdesktop.beansbinding.BeanProperty.create("enabled"));
        bindingGroup.addBinding(binding);

        cmbClient.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        cmbClient.setRenderer(produitListCellRenderer1);

        org.jdesktop.swingbinding.JComboBoxBinding jComboBoxBinding = org.jdesktop.swingbinding.SwingBindings.createJComboBoxBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, clientList, cmbClient);
        bindingGroup.addBinding(jComboBoxBinding);
        binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, this, org.jdesktop.beansbinding.ELProperty.create("${currentCommande == null}"), cmbClient, org.jdesktop.beansbinding.BeanProperty.create("enabled"));
        bindingGroup.addBinding(binding);

        cmbClient.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cmbClientActionPerformed(evt);
            }
        });

        produitListCellRenderer1.setText("produitListCellRenderer1");

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addComponent(dateCmdLabel)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jdate, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(163, 163, 163)
                        .addComponent(produitListCellRenderer1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addComponent(refLabel)
                        .addGap(20, 20, 20)
                        .addComponent(txtRef, javax.swing.GroupLayout.PREFERRED_SIZE, 191, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(clientIdLabel)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(cmbClient, javax.swing.GroupLayout.PREFERRED_SIZE, 192, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(refLabel)
                    .addComponent(txtRef, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(clientIdLabel)
                    .addComponent(cmbClient, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(dateCmdLabel, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 24, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(jdate, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(produitListCellRenderer1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))))
        );

        jPanel4.setBorder(javax.swing.BorderFactory.createTitledBorder("Lignes commande"));

        jTable1.setColumnSelectionAllowed(true);
        jTable1.setSelectionMode(javax.swing.ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);

        org.jdesktop.beansbinding.ELProperty eLProperty = org.jdesktop.beansbinding.ELProperty.create("${currentCommande.lignCmdCollection}");
        org.jdesktop.swingbinding.JTableBinding jTableBinding = org.jdesktop.swingbinding.SwingBindings.createJTableBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, this, eLProperty, jTable1);
        org.jdesktop.swingbinding.JTableBinding.ColumnBinding columnBinding = jTableBinding.addColumnBinding(org.jdesktop.beansbinding.ELProperty.create("${commande}"));
        columnBinding.setColumnName("Commande");
        columnBinding.setColumnClass(entity.Commande.class);
        columnBinding = jTableBinding.addColumnBinding(org.jdesktop.beansbinding.ELProperty.create("${hauteur}"));
        columnBinding.setColumnName("Hauteur");
        columnBinding.setColumnClass(Double.class);
        columnBinding = jTableBinding.addColumnBinding(org.jdesktop.beansbinding.ELProperty.create("${largeur}"));
        columnBinding.setColumnName("Largeur");
        columnBinding.setColumnClass(Double.class);
        columnBinding = jTableBinding.addColumnBinding(org.jdesktop.beansbinding.ELProperty.create("${lignCmdPK}"));
        columnBinding.setColumnName("Lign Cmd PK");
        columnBinding.setColumnClass(entity.LignCmdPK.class);
        columnBinding = jTableBinding.addColumnBinding(org.jdesktop.beansbinding.ELProperty.create("${marge}"));
        columnBinding.setColumnName("Marge");
        columnBinding.setColumnClass(Double.class);
        columnBinding = jTableBinding.addColumnBinding(org.jdesktop.beansbinding.ELProperty.create("${prixVendu}"));
        columnBinding.setColumnName("Prix Vendu");
        columnBinding.setColumnClass(Double.class);
        columnBinding = jTableBinding.addColumnBinding(org.jdesktop.beansbinding.ELProperty.create("${produit}"));
        columnBinding.setColumnName("Produit");
        columnBinding.setColumnClass(entity.Produit.class);
        columnBinding = jTableBinding.addColumnBinding(org.jdesktop.beansbinding.ELProperty.create("${qte}"));
        columnBinding.setColumnName("Qte");
        columnBinding.setColumnClass(Integer.class);
        columnBinding = jTableBinding.addColumnBinding(org.jdesktop.beansbinding.ELProperty.create("${total}"));
        columnBinding.setColumnName("Total");
        columnBinding.setColumnClass(Double.class);
        bindingGroup.addBinding(jTableBinding);
        jTableBinding.bind();binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, this, org.jdesktop.beansbinding.ELProperty.create("${currentCommande != null}"), jTable1, org.jdesktop.beansbinding.BeanProperty.create("enabled"));
        bindingGroup.addBinding(binding);

        jScrollPane1.setViewportView(jTable1);
        jTable1.getColumnModel().getSelectionModel().setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
        if (jTable1.getColumnModel().getColumnCount() > 0) {
            jTable1.getColumnModel().getColumn(2).setMinWidth(0);
            jTable1.getColumnModel().getColumn(2).setPreferredWidth(0);
            jTable1.getColumnModel().getColumn(2).setMaxWidth(0);
        }

        cmbPdt.setName("cmbClient"); // NOI18N
        cmbPdt.setRenderer(produitListCellRenderer1);

        jComboBoxBinding = org.jdesktop.swingbinding.SwingBindings.createJComboBoxBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, produitList, cmbPdt);
        bindingGroup.addBinding(jComboBoxBinding);
        binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, jTable1, org.jdesktop.beansbinding.ELProperty.create("${selectedElement.produit}"), cmbPdt, org.jdesktop.beansbinding.BeanProperty.create("selectedItem"));
        bindingGroup.addBinding(binding);

        cmbPdt.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cmbPdtActionPerformed(evt);
            }
        });

        lblpdt.setText("Produit: ");

        txtHauteur.setName("txtHauteur"); // NOI18N

        binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, jTable1, org.jdesktop.beansbinding.ELProperty.create("${selectedElement.hauteur}"), txtHauteur, org.jdesktop.beansbinding.BeanProperty.create("text"));
        bindingGroup.addBinding(binding);

        txtHauteur.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                txtHauteurActionPerformed(evt);
            }
        });
        txtHauteur.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyTyped(java.awt.event.KeyEvent evt) {
                txtHauteurKeyTyped(evt);
            }
        });

        txtLargeur.setName("txtLargeur"); // NOI18N

        binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, jTable1, org.jdesktop.beansbinding.ELProperty.create("${selectedElement.largeur}"), txtLargeur, org.jdesktop.beansbinding.BeanProperty.create("text"));
        bindingGroup.addBinding(binding);

        txtQte.setName("txtQte"); // NOI18N

        binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, jTable1, org.jdesktop.beansbinding.ELProperty.create("${selectedElement.qte}"), txtQte, org.jdesktop.beansbinding.BeanProperty.create("text"));
        bindingGroup.addBinding(binding);

        jLabel1.setText("Hauteur");

        jLabel2.setText("Largeur");

        lblQte.setText("Quantité");

        btnValid.setText("Valider");
        btnValid.setName("txtValider"); // NOI18N
        btnValid.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnValidActionPerformed(evt);
            }
        });

        btnValid1.setText("Valider");
        btnValid1.setName("txtValider"); // NOI18N
        btnValid1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnValid1ActionPerformed(evt);
            }
        });

        lblMsg.setFont(new java.awt.Font("Tahoma", 3, 11)); // NOI18N
        lblMsg.setForeground(new java.awt.Color(255, 0, 0));
        lblMsg.setText("Veuillez remplir les champs obligatoires");
        lblMsg.setVerticalAlignment(javax.swing.SwingConstants.TOP);

        javax.swing.GroupLayout jPanel4Layout = new javax.swing.GroupLayout(jPanel4);
        jPanel4.setLayout(jPanel4Layout);
        jPanel4Layout.setHorizontalGroup(
            jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel4Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel4Layout.createSequentialGroup()
                        .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(lblpdt)
                                    .addComponent(jLabel1))
                                .addComponent(jLabel2))
                            .addComponent(lblQte))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(cmbPdt, javax.swing.GroupLayout.PREFERRED_SIZE, 192, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                                .addComponent(txtQte, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 164, Short.MAX_VALUE)
                                .addComponent(txtLargeur, javax.swing.GroupLayout.Alignment.LEADING)
                                .addComponent(txtHauteur, javax.swing.GroupLayout.Alignment.LEADING))
                            .addGroup(jPanel4Layout.createSequentialGroup()
                                .addComponent(btnValid, javax.swing.GroupLayout.PREFERRED_SIZE, 86, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(btnValid1, javax.swing.GroupLayout.PREFERRED_SIZE, 86, javax.swing.GroupLayout.PREFERRED_SIZE)))
                        .addGap(8, 8, 8)
                        .addComponent(jScrollPane1))
                    .addComponent(lblMsg, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap())
        );
        jPanel4Layout.setVerticalGroup(
            jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel4Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(lblMsg)
                .addGap(14, 14, 14)
                .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel4Layout.createSequentialGroup()
                        .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(lblpdt)
                            .addComponent(cmbPdt, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(txtHauteur, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel1))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(txtLargeur, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel2))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(txtQte, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(lblQte))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(btnValid)
                            .addComponent(btnValid1)))
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 152, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(0, 33, Short.MAX_VALUE))
        );

        jPanel3.setBorder(javax.swing.BorderFactory.createTitledBorder("Réglement: "));

        totalLabel.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        totalLabel.setText("Total:");

        sommePayeLabel.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        sommePayeLabel.setText("Somme Paye:");

        creditLabel.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        creditLabel.setText("Credit:");

        beneficeLabel.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        beneficeLabel.setText("Benefice:");

        txtTotal.setEditable(false);
        txtTotal.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.NumberFormatter()));
        txtTotal.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N

        txtsomme.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.NumberFormatter()));
        txtsomme.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N

        binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, this, org.jdesktop.beansbinding.ELProperty.create("${currentCommande != null}"), txtsomme, org.jdesktop.beansbinding.BeanProperty.create("enabled"));
        bindingGroup.addBinding(binding);

        txtBenefice.setEditable(false);
        txtBenefice.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.NumberFormatter()));
        txtBenefice.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N

        txtCredit.setEditable(false);
        txtCredit.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.NumberFormatter()));
        txtCredit.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N

        javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
        jPanel3.setLayout(jPanel3Layout);
        jPanel3Layout.setHorizontalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel3Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(totalLabel)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(txtTotal, javax.swing.GroupLayout.PREFERRED_SIZE, 97, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(sommePayeLabel)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(txtsomme, javax.swing.GroupLayout.PREFERRED_SIZE, 98, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(beneficeLabel)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(txtBenefice, javax.swing.GroupLayout.PREFERRED_SIZE, 113, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(creditLabel)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(txtCredit, javax.swing.GroupLayout.PREFERRED_SIZE, 131, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(140, Short.MAX_VALUE))
        );
        jPanel3Layout.setVerticalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel3Layout.createSequentialGroup()
                .addContainerGap(19, Short.MAX_VALUE)
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(creditLabel)
                        .addComponent(txtCredit, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(beneficeLabel)
                        .addComponent(totalLabel)
                        .addComponent(sommePayeLabel)
                        .addComponent(txtTotal, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(txtsomme, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(txtBenefice, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap())
        );

        deleteButton.setBackground(new java.awt.Color(255, 153, 153));
        deleteButton.setText("Delete");
        deleteButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                deleteButtonActionPerformed(evt);
            }
        });

        deleteButton1.setText("Delete");
        deleteButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                deleteButton1ActionPerformed(evt);
            }
        });

        deleteButton2.setText("Delete");
        deleteButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                deleteButton2ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(10, 10, 10)
                .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
            .addGroup(layout.createSequentialGroup()
                .addGap(10, 10, 10)
                .addComponent(jPanel3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(0, 0, Short.MAX_VALUE))
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel4, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(deleteButton, javax.swing.GroupLayout.PREFERRED_SIZE, 90, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(0, 0, 0)
                .addComponent(deleteButton2, javax.swing.GroupLayout.PREFERRED_SIZE, 90, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(0, 0, 0)
                .addComponent(deleteButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 90, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel4, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(deleteButton, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(deleteButton2, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(deleteButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        bindingGroup.bind();

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void refreshButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_refreshButtonActionPerformed
        //        entityManager.getTransaction().rollback();
        //        entityManager.getTransaction().begin();
        //        java.util.Collection data = query.getResultList();
        //        for (Object entity : data) {
        //            entityManager.refresh(entity);
        //        }
        //        list.clear();
        //        list.addAll(data);
    }//GEN-LAST:event_refreshButtonActionPerformed

    private void deleteButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_deleteButtonActionPerformed
        //        int[] selected = masterTable.getSelectedRows();
        //        List<entity.Commande> toRemove = new ArrayList<entity.Commande>(selected.length);
        //        for (int idx = 0; idx < selected.length; idx++) {
            //            entity.Commande c = list.get(masterTable.convertRowIndexToModel(selected[idx]));
            //            toRemove.add(c);
            //            entityManager.remove(c);
            //        }
        //        list.removeAll(toRemove);
    }//GEN-LAST:event_deleteButtonActionPerformed

    private void deleteButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_deleteButton1ActionPerformed
        // TODO add your handling code here:

        entity.Commande c = this.currentCommande;
        Collection<entity.LignCmd> ls = c.getLignCmdCollection();
        if (ls == null) {
            ls = new LinkedList<entity.LignCmd>();
            c.setLignCmdCollection((List) ls);
        }
        entity.LignCmd l = new entity.LignCmd();
        entityManager.persist(l);
        l.setCommande(c);
        ls.add(l);
    }//GEN-LAST:event_deleteButton1ActionPerformed

    private void deleteButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_deleteButton2ActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_deleteButton2ActionPerformed

    private void btnValid1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnValid1ActionPerformed
        if (cmbPdt.getSelectedIndex() != -1) {
            if (!txtLargeur.getText().trim().isEmpty() && !txtHauteur.getText().trim().isEmpty()) {
                double suf = Double.parseDouble(txtLargeur.getText()) * Double.parseDouble(txtHauteur.getText());
                int qte = (int) ((1 - ((suf * PARPA) - ((int) (suf * PARPA)))) + (suf * PARPA));
                txtQte.setText(String.valueOf(qte));
            } else if (!txtLargeur.getText().trim().isEmpty()) {
                int qte = (int) ((1 - ((Double.parseDouble(txtLargeur.getText()) * LOURDI) - ((int) (Double.parseDouble(txtLargeur.getText()) * LOURDI)))) + (Double.parseDouble(txtLargeur.getText()) * LOURDI));
                txtQte.setText(String.valueOf(qte));
            }
        }
        if (!validFormLignCmd()) {
            return;
        }
        ((DefaultTableModel) jTable1.getModel()).addRow(new Object[]{((entity.Produit) cmbPdt.getSelectedItem()).getLibelle(),
            ((entity.Produit) cmbPdt.getSelectedItem()),
            ((entity.Produit) cmbPdt.getSelectedItem()).getPrixVendre(),
            txtQte.getText(),
            txtHauteur.getText(),
            txtLargeur.getText(),
            ((entity.Produit) cmbPdt.getSelectedItem()).getPrixVendre() * Integer.parseInt(txtQte.getText()),
            (((entity.Produit) cmbPdt.getSelectedItem()).getPrixVendre() - ((entity.Produit) cmbPdt.getSelectedItem()).getPrixAchat()) * Integer.parseInt(txtQte.getText())
        });
        //        int row = jTable1.getRowCount()- 1;
        //        jTable1.setRowSelectionInterval(row, row);
        //jTable1.scrollRectToVisible(jTable1.getCellRect(row, 0, true));
    }//GEN-LAST:event_btnValid1ActionPerformed

    private void btnValidActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnValidActionPerformed
        // TODO add your handling code here:
        if (jTable1.getSelectedRows().length == 0) {
            entity.LignCmd lc = new LignCmd();
            lc.setHauteur(12.90);
            this.getCurrentCommande().getLignCmdCollection().add(lc);
            int row = getCurrentCommande().getLignCmdCollection().size() - 1;
            jTable1.setRowSelectionInterval(row, row);
            jTable1.scrollRectToVisible(jTable1.getCellRect(row, 0, true));
        }
    }//GEN-LAST:event_btnValidActionPerformed

    private void txtHauteurKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txtHauteurKeyTyped
        // TODO add your handling code here:
    }//GEN-LAST:event_txtHauteurKeyTyped

    private void txtHauteurActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_txtHauteurActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_txtHauteurActionPerformed

    private void cmbPdtActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cmbPdtActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_cmbPdtActionPerformed

    private void cmbClientActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cmbClientActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_cmbClientActionPerformed

    void createJtable() {
        Object[][] rowData = {};
        Object[] columnNames = {"produit_id", "id_pdt", "prix_vendu", "qte", "hauteur", "largeur", "total", "marge"};

        DefaultTableModel listTableModel;
        listTableModel = new DefaultTableModel(rowData, columnNames);
        jTable1.setModel(listTableModel);
        jTable1.setAutoResizeMode(JTable.AUTO_RESIZE_ALL_COLUMNS);
        //Set header text
        jTable1.getColumn("produit_id").setHeaderValue("Produit");
        jTable1.getColumn("prix_vendu").setHeaderValue("Prix");
        jTable1.getColumn("qte").setHeaderValue("Quantitée");
        jTable1.getColumn("hauteur").setHeaderValue("Hauteur");
        jTable1.getColumn("largeur").setHeaderValue("Largeur");
        jTable1.getColumn("marge").setHeaderValue("La marge");
        jTable1.getColumn("total").setHeaderValue("Total");
        //jTable1.getColumn("produit_id_or").setHeaderValue("");
        // update some features.
        jTable1.getColumn("id_pdt").setMaxWidth(0);
        jTable1.getColumn("id_pdt").setMinWidth(0);
        jTable1.getColumn("id_pdt").setPreferredWidth(0);
//        jTable1.setRowSelectionAllowed(true);
//        jTable1.setCellSelectionEnabled(true);
        //set
    }
    boolean validFormLignCmd() {
        boolean isInvalid = true;
        boolean isNotEmpty = true;
        System.out.print("+++++++++++++++++++++++++++ " + txtQte.getText().trim());
        if (txtQte.getText().trim().isEmpty()) {
            txtQte.setBorder(BorderFactory.createLineBorder(Color.RED));
            lblQte.setForeground(Color.RED);
            isNotEmpty = false;
        } else {
            try {
                Integer.parseInt(txtQte.getText());
                txtQte.setBorder(BorderFactory.createBevelBorder(1));
                lblQte.setForeground(Color.BLACK);
            } catch (Exception ex) {
                txtQte.setBorder(BorderFactory.createLineBorder(Color.RED));
                lblQte.setForeground(Color.RED);
                isInvalid = false;
            }
        }
        if (cmbPdt.getSelectedIndex() == -1) {
            cmbPdt.setBorder(BorderFactory.createLineBorder(Color.RED));
            lblpdt.setForeground(Color.RED);
            isNotEmpty = false;
        } else {
            cmbPdt.setBorder(BorderFactory.createEtchedBorder());
            lblpdt.setForeground(Color.BLACK);
        }

        boolean temp;
        if (!isNotEmpty) {
            temp = isNotEmpty;
            lblMsg.setText("Veuillez remplir les champs obligatoires");
        } else {
            temp = isInvalid;
            lblMsg.setText("Les information saisi sont incorrecte");
        }
        lblMsg.setVisible(!temp);
        return temp;
    }

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(CommandeDetaillesForm.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(CommandeDetaillesForm.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(CommandeDetaillesForm.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(CommandeDetaillesForm.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new CommandeDetaillesForm().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel beneficeLabel;
    private javax.swing.JButton btnValid;
    private javax.swing.JButton btnValid1;
    private javax.swing.JLabel clientIdLabel;
    private java.util.List<entity.Client> clientList;
    private javax.persistence.Query clientQuery;
    private javax.swing.JComboBox cmbClient;
    private javax.swing.JComboBox cmbPdt;
    private javax.swing.JLabel creditLabel;
    private javax.swing.JLabel dateCmdLabel;
    private javax.swing.JButton deleteButton;
    private javax.swing.JButton deleteButton1;
    private javax.swing.JButton deleteButton2;
    private javax.persistence.EntityManager gestionPUEntityManager;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JPanel jPanel4;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable jTable1;
    private javax.swing.JSpinner jdate;
    private javax.swing.JLabel lblMsg;
    private javax.swing.JLabel lblQte;
    private javax.swing.JLabel lblpdt;
    private java.util.List<entity.LignCmd> lignCmdList;
    private java.util.List<entity.LignCmd> lignCmdList1;
    private javax.persistence.Query lignCmdQuery;
    private javax.persistence.Query lignCmdQuery1;
    private java.util.List<entity.Produit> produitList;
    private presentation.ProduitListCellRenderer produitListCellRenderer1;
    private javax.persistence.Query produitQuery;
    private javax.swing.JLabel refLabel;
    private javax.swing.JButton refreshButton;
    private javax.swing.JLabel sommePayeLabel;
    private javax.swing.JLabel totalLabel;
    private javax.swing.JFormattedTextField txtBenefice;
    private javax.swing.JFormattedTextField txtCredit;
    private javax.swing.JTextField txtHauteur;
    private javax.swing.JTextField txtLargeur;
    private javax.swing.JTextField txtQte;
    private javax.swing.JTextField txtRef;
    private javax.swing.JFormattedTextField txtTotal;
    private javax.swing.JFormattedTextField txtsomme;
    private org.jdesktop.beansbinding.BindingGroup bindingGroup;
    // End of variables declaration//GEN-END:variables

    /**
     * @return the currentCommande
     */
    public entity.Commande getCurrentCommande() {
        return currentCommande;
    }

    /**
     * @param currentCommande the currentCommande to set
     */
    public void setCurrentCommande(entity.Commande currentCommande) {
        this.currentCommande = currentCommande;
    }

}
